package com.better.frontend.controller.exam;

import com.better.backend.mqReceiverService.examReceiver.model.ExamModel;
import com.better.backend.mqReceiverService.examReceiver.model.ExamUnitModel;
import com.better.backend.mqReceiverService.examReceiver.model.SubjectScoreModel;
import com.better.common.base.ServiceResponse;
import com.better.common.exception.ExceptionEnum;
import com.better.common.jwtauth.secruity.JWTUserDetails;
import com.better.common.utils.ServiceResponseHandle;
import com.better.common.utils.SessionUtils;
import com.better.consist.CalTypeEnum;
import com.better.consist.ExamEnum;
import com.better.frontend.Iservice.exam.ExamService;
import com.better.frontend.Iservice.paper.PaperService;
import com.better.frontend.model.exam.AddExamSubjectParam;
import com.better.frontend.model.exam.CreateExamParam;
import com.better.frontend.repository.grade.GradeRepsitory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 考试管理
 * Created by wfm on 2017/11/19.
 */
@Api(value = "创建考试信息", tags = "创建考试信息接口")
@RestController
@RequestMapping("/api/examManager")
public class ExamManagerController {
    private static Log log = LogFactory.getLog(ExamManagerController.class);
    @Resource
    private ExamService examService;
    @Resource
    private GradeRepsitory gradRepsitory;
    @Resource
    private PaperService paperService;

    @ApiOperation(value = "创建考试", notes = "创建考试")
    @RequestMapping(value = "/createExam", method = RequestMethod.POST)
    public ServiceResponse<?> createExam(@RequestBody CreateExamParam createExamParam) {
        JWTUserDetails user = SessionUtils.getUserInfo();
        ExamModel examModel = new ExamModel();
        if (StringUtils.isNotEmpty(createExamParam.getId())) {
            examModel = examService.findByExampId(createExamParam.getId());
            examModel.setExamName(createExamParam.getExamName());
            examModel.setStartDate(createExamParam.getStartDate());
            examModel.setEndDate(createExamParam.getEndDate());
            examModel.preUpdate();
        } else {
            BeanUtils.copyProperties(createExamParam, examModel);
            examModel.setStatus(ExamEnum.EXAM_STATUS_PAPER.getCode().toString());
            examModel.setStatusName(ExamEnum.EXAM_STATUS_PAPER.getName());
            examModel.setCreate_by(user.getUsername());
            examModel.setSchoolId(user.getSchoolId());
            examModel.setSchoolName(user.getSchoolName());
            examModel.preInsert();
        }
        if (CalTypeEnum.examtype_class.equals(createExamParam.getExamType())) {
            examModel.setGradeId(user.getGradeId());
            examModel.setGradeName(user.getGradeName());
        }
        return ServiceResponseHandle.success(examService.saveExmp(examModel));
    }

/*    @ApiOperation(value = "创建考试", notes = "创建考试")
    @RequestMapping(value = "/createExam", method = RequestMethod.POST)
    public ServiceResponse<?> createExam111(@RequestBody CreateExamParam createExamParam) {
        ExamModel examModel = new ExamModel();
        BeanUtils.copyProperties(createExamParam, examModel);
        examModel.setStatus(ExamEnum.EXAM_STATUS_PAPER.getCode().toString());
        examModel.setStatusName(ExamEnum.EXAM_STATUS_PAPER.getName());
        JWTUserDetails user = SessionUtils.getUserInfo();
        if (StringUtils.isEmpty(examModel.getId())) {
            examModel.preInsert();
        } else {
            ExamModel ordExam = examService.findByExampId(examModel.getId());
            BeanUtils.copyProperties(ordExam, examModel);
            examModel.setExamUnitModels(ordExam.getExamUnitModels());
            examModel.preUpdate();
        }
        examModel.setCreate_by(user.getUsername());
        examModel.setSchoolId(user.getSchoolId());
        examModel.setSchoolName(user.getSchoolName());
        if (CalTypeEnum.examtype_class.equals(createExamParam.getExamType())) {
            examModel.setGradeId(user.getGradeId());
            examModel.setGradeName(user.getGradeName());
        }
        return ServiceResponseHandle.success(examService.saveExmp(examModel));
    }*/

    @ApiOperation(value = "创建考试年级科目信息", notes = "创建考试年级科目信息,只需要传入考试id,和考试科目相关信息")
    @RequestMapping(value = "/updateExam", method = RequestMethod.POST)
    public ServiceResponse<?> updateExam(@RequestBody AddExamSubjectParam addExamSubjectParam) {
        ExamModel examInfo = examService.findByExampId(addExamSubjectParam.getId());
        List<ExamUnitModel> list = addExamSubjectParam.getExamUnitModels();
        list = list.stream().filter(l -> Objects.nonNull(l)).collect(Collectors.toList());
        if (CalTypeEnum.examtype_class.equals(examInfo.getExamType())) {
            JWTUserDetails user = SessionUtils.getUserInfo();
            for (ExamUnitModel examUntilModel : list) {
                examUntilModel.setUnitId(user.getClassId());
                examUntilModel.setUnitName(user.getClassName());
                //过滤为空的参考科目
                List<SubjectScoreModel> sl = examUntilModel.getSubjectScoreModels();
                sl = sl.stream().filter(s -> Objects.nonNull(s)).collect(Collectors.toList());
                examUntilModel.setSubjectScoreModels(sl);
            }
        } else {
            //过滤为空的科目
            for (ExamUnitModel e : list) {
                List<SubjectScoreModel> subjectScoreModels = e.getSubjectScoreModels();
                subjectScoreModels = subjectScoreModels.stream().filter(s -> Objects.nonNull(s)).collect(Collectors.toList());
                e.setSubjectScoreModels(subjectScoreModels);
            }
        }
        if (examInfo.getExamUnitModels() != null && examInfo.getExamUnitModels().size() > 0) {
            ServiceResponse sre = examService.compUnitSub(examInfo, list);
            if (ExceptionEnum.USER_CHOICE.getCode().equals(sre.getCode())) {
                return sre;
            }
        }
        examInfo.setExamUnitModels(list);//设置参考年级和相应的科目信息
        return ServiceResponseHandle.success(examService.updateExmp(examInfo));//更新
    }

    @ApiOperation(value = "创建考试年级科目信息", notes = "创建考试年级科目信息,只需要传入考试id,和考试科目相关信息")
    @RequestMapping(value = "/updateExamComp", method = RequestMethod.POST)
    public ServiceResponse<?> updateExamComp(@RequestBody AddExamSubjectParam addExamSubjectParam) {
        ExamModel examInfo = examService.findByExampId(addExamSubjectParam.getId());
        List<ExamUnitModel> list = addExamSubjectParam.getExamUnitModels();
        list = list.stream().filter(l -> Objects.nonNull(l)).collect(Collectors.toList());
        if (CalTypeEnum.examtype_class.equals(examInfo.getExamType())) {
            JWTUserDetails user = SessionUtils.getUserInfo();
            for (ExamUnitModel examUntilModel : list) {
                examUntilModel.setUnitId(user.getClassId());
                examUntilModel.setUnitName(user.getClassName());
                //过滤为空的参考科目
                List<SubjectScoreModel> sl = examUntilModel.getSubjectScoreModels();
                sl = sl.stream().filter(s -> Objects.nonNull(s)).collect(Collectors.toList());
                examUntilModel.setSubjectScoreModels(sl);
            }
        } else {
            //过滤为空的科目
            for (ExamUnitModel e : list) {
                List<SubjectScoreModel> subjectScoreModels = e.getSubjectScoreModels();
                subjectScoreModels = subjectScoreModels.stream().filter(s -> Objects.nonNull(s)).collect(Collectors.toList());
                e.setSubjectScoreModels(subjectScoreModels);
            }
        }
        //清除本次考试所有已导入的试卷信息
        paperService.deleteByExamId(examInfo.getId());
        examInfo.setExamUnitModels(list);//设置参考年级和相应的科目信息
        return ServiceResponseHandle.success(examService.updateExmp(examInfo));//更新
    }

    @ApiOperation(value = "发布考试", notes = "发布考试")
    @RequestMapping(value = "/publishExam", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResponse<?> publishExam(String examId) {
        if (log.isDebugEnabled()) {
            log.debug("调用接口:[publishExam],参数:[publishExam =]" + examId);
        }
        return examService.publishExam(examId);
    }

    @ApiOperation(value = "查询考试信息", notes = "查询考试信息")
    @RequestMapping(value = "/getExamInfo", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResponse<?> getExamInfo(String examId) {
        if (log.isDebugEnabled()) {
            log.debug("调用接口:[publishExam],参数:[publishExam =]" + examId);
        }
        return ServiceResponseHandle.success(examService.findByExampId(examId));
    }

    @ApiOperation(value = "根据考试ID，查询年级，班级信息", notes = "根据考试ID，查询年级，班级信息")
    @RequestMapping(value = "/getGradeClassInfo", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResponse<?> getGradeClassInfo(String examId) {
        if (log.isDebugEnabled()) {
            log.debug("调用接口:[getGradeClassInfo],参数:[getGradeClassInfo =]" + examId);
        }
        return examService.getGradeClassInfo(examId);
    }

}
